var pageii = null;
var grid = null;
$(function() {
	grid = lyGrid({
		pagId : 'paging',
		l_column : [ {
			colkey : "code",
			name : "物料编号",
		}, {
			colkey : "name",
			name : "物料名称",
		}, {
			colkey : "specification",
			name : "物料规格",
		}],
		jsonUrl : rootPath + '/pi/getAssociatedByPage.shtml?id='+$("#c").val(),
		dymCol:true,
		checkbox : false,
		checkValue: "id",
		records: "list",
		totalPages: "pages",
		totalRecords: "total",
		pagecode: '10',
		pageNow : 'pageNum'
	});
	
	$("#addAccount").click("click", function() {
		addAccount();
	});
	$("#editAccount").click("click", function() {
		editAccount();
	});
	$("#ok").click("click", function() {
		associated();
	});
	$("#associated").click("click", function() {
		associated();
	});
	$("#search").click("click", function() {
		var sel = $("#billstateid option:selected").val();//获取下拉框数据
		var myv = $("#btime").val();//获取输入框数据
		//放出到input框中
		$("#myValue").val(sel+","+myv);
		var searchParams = $("#myValue").serializeJson();
		grid.setOptions({
			data : searchParams
		});
	});
});
function editAccount() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		layer.msg("只能选中一个");
		return;
	}
	pageii = layer.open({
		title : "编辑项目检验",
		type : 2,
		area : [ "800px", "100%" ],
		content : rootPath + '/pi/pi_editUI.shtml?id=' + cbox
	});
}

function associated() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		layer.msg("只能选中一个");
		return;
	}
	pageii = layer.open({
		title : "关联项目",
		type : 2,
		area : [ "800px", "100%" ],
		content : rootPath + '/pi/associated.shtml?id=' + cbox
	});
}
function addAccount() {
	pageii = layer.open({
		title : "新增项目检验",
		type : 2,
		area : [ "800", "100%" ],
		content : rootPath + '/pi/openAdd.shtml'
	});
}
function associated() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox == "") {
		layer.msg("请选择关联项！！");
		return;
	}
	var join = cbox.join(",");
	var temp = join+","+$("#myname").val();
	layer.confirm('是否关联？', function(index) {
		var url = rootPath + '/pi/relevance.shtml';
		var s = CommnUtil.ajax(url, {
			ids : temp
		}, "json");
		if (s == "success") {
			layer.msg('关联成功');
			grid.loadData();
		} else {
			layer.msg('关联失败');
		}
	});
}

function exportExcel(excelName){
    // var mytable = "";//需要导出的数据
    var fileName = excelName+".xlsx"

    var cbox = grid.getSelectedCheckbox();
    if (cbox == "") {//没选则导出所有数据
        var mytable = document.getElementById("mytable");//获取table
        var sheet = XLSX.utils.table_to_sheet(mytable);
    } else {//第二种：导出指定的行

        //获取表头标题，就是表中第一行，序号不存在
        var valueTd= document.getElementById ("mytable").rows[0];

        //复制节点
        var clone_valueTd = valueTd.cloneNode(true);
        // debugger;

        //根据选中行，动态拼接table
        // var tableData = "<table><tr>" + valueTd.innerHTML + "</tr>";
        var table = document.createElement('table');
        table.id = "table1";
        table.appendChild(clone_valueTd);//使用原有节点会对原有dom造成影响

        //获取所有表中所有checkbox
        var inputs = document.getElementById("mytable").getElementsByTagName("input");
        console.log("inputs.length:"+inputs.length);
        for(var i=0;i<inputs.length;i++) {//获取选中行
            if (inputs[i].type == "checkbox") {
                if (inputs[i].checked) {
                    var checkedRow = inputs[i];
                    var tr = checkedRow.parentNode.parentNode;
                    var clone_tr = tr.cloneNode(true);//复制
                    console.log(tr.cells[0].innerText);
                    if(tr.cells[0].innerText != ""){//序号不为空则拼接，为空说明是表头标题
                        // tableData += "<tr>" + tr.innerHTML + "</tr>";
                        table.appendChild(clone_tr)
                    }
                    // console.log("为空是表头，不拼接")
                    // console.log("tr...:"+tr);
                }
            }
        }

        // tableData += "</table>";
        // mytable = document.createElement(tableData);
        var sheet = XLSX.utils.table_to_sheet(table);
    }

    openDownloadDialog(sheet2blob(sheet), fileName);
}